var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
Page({
  data: {
    userInfo: {},
    role: false,
    token: null,
    type: '3',
    tx: false,
    hasNewSpread: "",
    newOrderTabs: null,
    totalShare: "0.00",
    txInfo: {},
    status0:false,
    status1:false,
    status6:false,
    status3:false
  },
  onLoad: function(options) {
    var that = this;
  },
  onReady: function() {

  },
  onShow: function() {
    console.log("on show")
    let token = wx.getStorageSync('token');
    let that = this;
    // 页面显示
    wx.getUserInfo({
      success: function(res) {
        that.setData({
          userInfo: res.userInfo,
          token: token
        })
      }
    })
    //判断是否登录
    if (token == '' || !token) {
      token = null;
    } else {
      //已经登录，获取用户信息
      var customerId = wx.getStorageSync("customerId");
      util.request(api.UserDetail, {
        id: customerId
      }).then(function(res) {
        if (res.code === '200') {
          that.setData({
            customerInfo: res.datas,
            type: res.datas.type
          })
          wx.setStorageSync("type", res.datas.type);
          wx.setStorageSync("token", res.datas.token);
          wx.setStorageSync("auth", res.datas.auth);
          wx.setStorageSync("authType", res.datas.authType);

          if (res.datas.type == '1' || res.datas.type == '0') {
            wx.setStorageSync("shareId", res.datas.id);
          }
          if (res.datas.recommendId) {
            wx.setStorageSync("shareId", res.datas.recommendId);
          }
          wx.setStorageSync("auth", res.datas.auth ? res.datas.auth : "0")
          that.getData();
        }
      });
    }
  },
  getData: function() {
    console.log("get data")
    //查询是否有新分销订单
    var that = this;
    util.request(api.HasNew, {
      token: wx.getStorageSync("token"),
      status: "1"
    }).then(function(res) {
      if (res.code == '200') {
        var hasNewOrder = res.datas && (res.datas.cur0 == true || res.datas.cur1 == true || res.datas.cur2 == true || res.datas.cur3 == true);
        that.setData({
          hasNewOrder: hasNewOrder,
          newOrderTabs: res.datas
        })
      } else if (res.code == '500' && res.datas == 'authfaild') {
        wx.removeStorageSync("token");
        wx.showModal({
          title: '登录过时，请重新登录',
          content: '',
          success: function(options) {
            if (options.confirm) {
              wx.navigateTo({
                url: '/pages/auth/register/register',
              })
            }
          }
        })
      }
    });
    //查询是否有新单
    util.request(api.OrderHasNew, {
      token: wx.getStorageSync("token")
    }).then(function(res) {
 
      if (res.code == '200') {
        console.log("OrderHasNew:"+res.datas.status0)
        if (res.datas.status0 > 0) {
          that.setData({
            status0: true
          })
        }else{
          that.setData({
            status0: false
          })
        }
        if (res.datas.status1 > 0) {
          that.setData({
            status1: true
          })
        } else {
          that.setData({
            status1: false
          })
        }
        if (res.datas.status6 > 0) {
          that.setData({
            status6: true
          })
        } else {
          that.setData({
            status6: false
          })
        }
        if (res.datas.status3 > 0) {
          that.setData({
            status3: true
          })
        } else {
          that.setData({
            status3: false
          })
        }

      } else if (res.code == '500' && res.datas == 'authfaild') {
        wx.removeStorageSync("token");
        wx.showModal({
          title: '登录过时，请重新登录',
          content: '',
          success: function(options) {
            if (options.confirm) {
              wx.navigateTo({
                url: '/pages/auth/register/register',
              })
            }
          }
        })
      }
    });

    util.request(api.SpreadHasNew, {
      token: wx.getStorageSync("token")
    }).then(function(res) {
      if (res.code == '200') {
        that.setData({
          hasNewSpread: res.datas
        })
      }
    });

    //获取分利总数
    util.request(api.TotalShare, {
      token: wx.getStorageSync("token")
    }).then(function(res) {
      if (res.code == '200') {
        that.setData({
          totalShare: res.datas
        })
      } else if (res.code == '500' && res.datas == 'authfail') {
        wx.showModal({
          title: '登录过期，重新登录?',
          content: '',
          success: function(options) {
            if (options.confirm) {
              wx.navigateTo({
                url: '/pages/auth/register/register',
              })
            }
          }
        })
      } else {

      }
    })
  },
  onHide: function() {
    // 页面隐藏

  },
  onUnload: function() {
    // 页面关闭
  },
  exitLogin: function() {
    var token = wx.getStorageSync("token");
    if (token) {
      wx.showModal({
        title: '',
        confirmColor: '#b4282d',
        content: '退出登录？',
        success: function(res) {
          if (res.confirm) {
            wx.removeStorageSync('token');
            wx.removeStorageSync('openId');
            wx.removeStorageSync('userInfo');
            wx.removeStorageSync("type");
            wx.removeStorageSync("customerId");
            wx.switchTab({
              url: '/pages/index/index-new'
            });
          }
        }
      })
    }

  },
  bindOrder: function() {
    var token = wx.getStorageSync("token");
    if (!token || token == '') {
      wx.showModal({
        title: '您还未登录，是否立即去登录?',
        content: '',
        success: function(options) {
          if (options.confirm) {
            wx.navigateTo({
              url: '/pages/auth/register/register',
            })
          }
        }
      })
      return;
    }
    wx.navigateTo({
      url: '/pages/ucenter/order/order',
    })
  },
  bindCollect: function() {
    var token = wx.getStorageSync("token");
    if (!token || token == '') {
      wx.showModal({
        title: '您还未登录，是否立即去登录?',
        content: '',
        success: function(options) {
          if (options.confirm) {
            wx.navigateTo({
              url: '/pages/auth/register/register',
            })
          }
        }
      })
      return;
    }
    wx.navigateTo({
      url: '/pages/ucenter/collect/collect',
    })
  },
  bindCoupon: function() {
    var token = wx.getStorageSync("token");
    if (!token || token == '') {
      wx.showModal({
        title: '您还未登录，是否立即去登录?',
        content: '',
        success: function(options) {
          if (options.confirm) {
            wx.navigateTo({
              url: '/pages/auth/register/register',
            })
          }
        }
      })
      return;
    }
    wx.navigateTo({
      url: '/pages/ucenter/coupon/mycoupon',
    })
  },
  bindContact: function() {
    wx.makePhoneCall({
      phoneNumber: '4000282666',
    })
  },
  bindAboutUs: function() {
    wx.navigateTo({
      url: '/pages/ucenter/aboutus/index',
    })
  },
  fnChange: function(e) {
    let that = this;
    var role = !that.data.role;
    wx.setStorageSync("role", role);
    this.setData({
      role: role
    })
  },
  fnLogin: function(e) {
    wx.navigateTo({
      url: '/pages/auth/register/register',
    })
  },
  fnShare: function() {
    var token = wx.getStorageSync("token");
    if (!token || token == '') {
      wx.showModal({
        title: '您还未登录，是否立即去登录?',
        content: '',
        success: function(options) {
          if (options.confirm) {
            wx.navigateTo({
              url: '/pages/auth/register/register',
            })
          }
        }
      })
      return;
    }
    // var path = "/pages/index/index-new";
    //type=0，表示为供应商邀请分销商
    // app.fnShareGoods(path, 1, "../../static/images/toShare.png", "风光鱼旅游分销平台诚邀您加入", "0");

    wx.navigateTo({
      url: '/pages/ucenter/invite/index?shareType=0',
    })
  },
  fnSetting: function() {
    var that = this;
    var token = wx.getStorageSync("token");
    if (!token || token == '') {
      wx.showModal({
        title: '您还未登录，是否立即去登录?',
        content: '',
        success: function(options) {
          if (options.confirm) {
            wx.navigateTo({
              url: '/pages/auth/register/register',
            })
          }
        }
      })
      return;
    }
    if (that.data.customerInfo.auth != '1' && that.data.customerInfo.type != '0') {
      wx.showModal({
        title: '还未认证，是否立即去认证？',
        content: '',
        success: function(options) {
          if (options.confirm) {
            wx.navigateTo({
              url: '/pages/perCenter/disAuth/disAuth',
            })
          }
        }
      })
      return;
    }
    this.setData({
      isSetting: true
    })
  },
  fnClose: function(e) {
    this.setData({
      isSetting: false
    })
  },
  fnCloseTx: function(e) {
    this.setData({
      tx: false
    })
  },
  fnSureTx: function(e) {

    var that = this;
    var txInfo = that.data.txInfo;
    if (txInfo == null || txInfo.account == null || txInfo.account == "" || txInfo.accountName == null || txInfo.accountName == "" || that.data.price == '' || that.data.price == null || txInfo.remark == '' || txInfo.remark == null) {
      var msg = '请完善提现信息！'
      wx.showModal({
        title: '提示',
        content: msg,
      })
      return;
    }
    if (!util.checkBankNo(txInfo.account)) {
      msg = "银行账户无效!"
      wx.showModal({
        title: '提示',
        content: msg,
      })
      return;
    }
    wx.setStorageSync("txInfo", txInfo);
    if (parseFloat(this.data.customerInfo.divided) - parseFloat(this.data.price) < 0) {
      return;
    }
    var data = {
      price: this.data.price,
      account: this.data.txInfo.account,
      accountName: this.data.txInfo.accountName,
      remark: this.data.txInfo.remark,
      token: wx.getStorageSync("token")
    }
    wx.showLoading({
      title: '加载中。。。',
      mask: true
    })
    util.request(api.Tx, data).then(function(res) {
      wx.hideLoading();
      if (res.code === '200') {
        //关闭提现页面
        var customerInfo = that.data.customerInfo;
        customerInfo.divided = res.datas;
        that.setData({
          tx: false,
          customerInfo: customerInfo
        });

      }
    });
  },
  fnInputTxInfo: function(e) {
    var id = e.currentTarget.id;
    var txInfo = this.data.txInfo;
    txInfo['' + id] = e.detail.value;
    this.setData({
      txInfo: txInfo,
    });
    if ("divided" == id) {
      var price = e.detail.value;
      if (parseFloat(this.data.customerInfo.divided) - parseFloat(e.detail.value) < 0 || price < 0) {
        price = this.data.customerInfo.divided;
      }
      this.setData({
        price: price
      })
    }
  },
  fnInputNumber: function(e) {
    var value = e.detail.value;
    var customerInfo = this.data.customerInfo;
    customerInfo.dividedNumber = value;
    this.setData({
      customerInfo: customerInfo
    })
  },
  fnSure: function(e) {
    var that = this;
    var token = wx.getStorageSync("token");
    var data = {
      token: token,
      shareNumber: that.data.customerInfo.dividedNumber
    };
    if (that.data.customerInfo.dividedNumber > 0 && that.data.customerInfo.dividedNumber < 1) {
      util.request(api.UserSet, data).then(function(res) {
        if (res.code === '200') {
          //设置成功
          that.setData({
            isSetting: false
          })
          wx.showToast({
            title: '设置成功！',
          });
        } else {
          wx.showToast({
            title: '操作失败',
            duration: 1500,
            image: "/static/images/icon_error.png"
          })
        }
      });
    } else {
      wx.showToast({
        title: '数值不合法!',
        duration: 1500,
        image: "/static/images/icon_error.png"
      })
    }

  },
  fnToTx: function() {
    var that = this;
    if (that.data.customerInfo.auth == '0') {
      wx.showModal({
        title: '还未认证，是否立即去认证？',
        content: '',
        success: function(options) {
          if (options.confirm) {
            wx.navigateTo({
              url: '/pages/perCenter/disAuth/disAuth',
            })
          }
        }
      })
      return;
    }
    var divided = that.data.customerInfo.divided;
    var url = "/pages/perCenter/withdLast/withdLast?divided=" + divided;
    wx.navigateTo({
      url: url,
    });
  },
  fnTx: function(e) {
    var that = this;
    var shareMoney = that.data.customerInfo.divided;
    if (!shareMoney || shareMoney == 0) {
      wx.showModal({
        title: '无可用提现额度',
        content: '',
      })
      return;
    }
    var txInfo = wx.getStorageSync("txInfo");
    that.setData({
      tx: true,
      txInfo: txInfo == '' ? {} : txInfo,
      price: shareMoney
    })
  },
  fnEmpty: function() {},
  bindUpload: function(e) {
    var token = wx.getStorageSync("token");
    var that = this;
    if (!token || token == '') {
      wx.showModal({
        title: '您还未登录，是否立即去登录?',
        content: '',
        success: function(options) {
          if (options.confirm) {
            wx.navigateTo({
              url: '/pages/auth/register/register',
            })
          }
        }
      })
      return;
    }
    if (this.data.customerInfo.type == '0') {
      return;
    }
    wx.navigateTo({
      url: '/pages/perCenter/disAuth/disAuth?auth=' + that.data.customerInfo.auth,
    })
  },
  fnShowDetail: function() {
    var token = wx.getStorageSync("token");
    var that = this;
    if (!token || token == '') {
      wx.showModal({
        title: '您还未登录，是否立即去登录?',
        content: '',
        success: function(options) {
          if (options.confirm) {
            wx.navigateTo({
              url: '/pages/auth/register/register',
            })
          }
        }
      })
      return;
    }
    if (that.data.customerInfo.auth != '1' && that.data.customerInfo.type != '0') {
      wx.showModal({
        title: '还未认证，是否立即去认证？',
        content: '',
        success: function(options) {
          if (options.confirm) {
            wx.navigateTo({
              url: '/pages/perCenter/disAuth/disAuth',
            })
          }
        }
      })
      return;
    }
    var tabs = that.data.newOrderTabs;
    wx.navigateTo({
      url: '/pages/perCenter/disOrder/disOrder?cur0=' + tabs.cur0 + "&cur1=" + tabs.cur1 + "&cur2=" + tabs.cur2 + "&cur3=" + tabs.cur3,
    })
  },
  fnToOrderList: function(e) {
    var token = wx.getStorageSync("token");
    if (!token || token == '') {
      wx.showModal({
        title: '您还未登录，是否立即去登录?',
        content: '',
        success: function(options) {
          if (options.confirm) {
            wx.navigateTo({
              url: '/pages/auth/register/register',
            })
          }
        }
      })
      return;
    }
    var url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url,
    })
  },
  //进入邀请推广页面
  fnToShare: function(e) {
    var that = this;
    var token = wx.getStorageSync("token");
    if (!token || token == '') {
      wx.showModal({
        title: '您还未登录，是否立即去登录?',
        content: '',
        success: function(options) {
          if (options.confirm) {
            wx.navigateTo({
              url: '/pages/auth/register/register',
            })
          }
        }
      })
      return;
    }
    if (that.data.customerInfo.auth != '1' && that.data.customerInfo.type != 0) {
      wx.showModal({
        title: '还未认证，是否立即去认证？',
        content: '',
        success: function(options) {
          if (options.confirm) {
            wx.navigateTo({
              url: '/pages/perCenter/disAuth/disAuth',
            })
          }
        }
      })
      return;
    }
    var url = "/pages/perCenter/shareUsers/index";
    wx.navigateTo({
      url: url,
    })
  },
  fnToPerson: function() {
    var that = this;
    var token = wx.getStorageSync("token");
    if (token) {
      //已经登录，跳转到个人信息页面
      var url = "/pages/perCenter/personInfo/personInfo";
      var type = that.data.customerInfo.type;
      var name = that.data.customerInfo.realName;
      var telephone = that.data.customerInfo.mobilePhone;
      url = url + "?type=" + type +
        "&name=" + name +
        "&tel=" + telephone;
      wx.navigateTo({
        url: url,
      })
    } else {
      wx.showModal({
        title: '您还未登录，是否去登陆？',
        content: '',
        success: function(option) {
          if (option.confirm) {
            wx.navigateTo({
              url: '/pages/auth/register/register',
            })
          }
        }
      })
    }
  }
})